# _*_ coding:utf-8 _*_
"""
@author:lx
@file: statistics
@contact: lixiang-929@outlook.com
@time: 2019/03/23
"""
import cv2
import numpy as np
import os
import time
import matplotlib.pyplot as plt

# data_path = ".//red_data//"
# data_path = ".//yellow//"
result_path = ".//result//"

def Statistics(orgin_image,ratio):
    start_time = time.time()
    image_pixValue = [0] * 256
    image_pixRatio = [0.00] * 256
    # image_name = os.path.join(data_path, orgin_image)
    # target_image = cv2.imread(image_name,0)
    target_image = cv2.imread(orgin_image, 0)
    # print(target_image.shape)
    # imin, imax = cv2.minMaxLoc(target_image)[:2]
    # thr = int((imin + imax) / 2)
    sum_pix = target_image.shape[0] * target_image.shape[1]
    sum = 0.0
    pix_value_ratio = 0

    if target_image.shape[0]!= 0 and target_image.shape[1]!= 0:
        target_image1 = cv2.Canny(target_image,50,100)
        # cv2.imshow('targe_image1',target_image1)
        for j in range(target_image1.shape[0]):
            for i in range(target_image1.shape[1]):
                pix_value = target_image[j,i]
                image_pixValue[int(pix_value)] = image_pixValue[int(pix_value)] + 1

        for k in range(256):
            sum = sum + image_pixValue[255 - k]
            # print("sum= %d" % sum)
            pix_ratio = float(sum) / sum_pix
            # print("pix_ratio= %f" % pix_ratio)
            image_pixRatio[255 - k] = pix_ratio

        for pix in range(256):
            if image_pixRatio[255 - pix] > ratio:
                pix_value_ratio = 255 - pix
                break
        print('pix_value_ratio=',pix_value_ratio)
        ret,target_image_THR = cv2.threshold(target_image,pix_value_ratio,255,cv2.THRESH_BINARY)
        # cv2.imshow("roi_THR",target_image_THR)

        file_name, file_format = os.path.splitext(orgin_image)
        red_THR = os.path.join(result_path, file_name + "_red_THR.jpg")
        # yellow_THR = os.path.join(result_path, file_name + "_THR.jpg")

        plt.subplot(131), plt.imshow(target_image1, "gray")
        plt.title("source image"), plt.xticks([]), plt.yticks([])
        plt.subplot(132), plt.hist(target_image1.ravel(), 256)
        plt.title("Histogram"), plt.xticks([]), plt.yticks([])
        plt.subplot(133), plt.imshow(target_image_THR, "gray")
        plt.title("the threshold is " + str(ret)), plt.xticks([]), plt.yticks([])
        cv2.imwrite(red_THR, target_image_THR)
        # cv2.imwrite(yellow_THR, target_image_THR)
        print("time={0:.3f}, {1:s}".format((time.time() - start_time), file_name))
        cv2.waitKey(10)
        plt.show()

    else:
        target_image_THR = np.zeros((0,0))
        target_image1 = np.zeros((0,0))

    return pix_value_ratio,target_image_THR,target_image1

if __name__ == "__main__":
    file = "red_thr1.jpg"
    Statistics(file,0.2)
    # data_list = os.listdir(data_path)
    # print(data_list)
    # for data in data_list:
    #     Statistics(data, 0.01)

